package com.vito.act.tool;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vito.base.util.Contants;
import com.vito.base.util.SpringApplicationContext;
import com.vito.base.util.StringUtil;
import com.vito.base.util.Validata;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.dao.OaActProcessMapper;
import com.vito.oa.act.event.service.ActivitiEvent;
import com.vito.oa.act.event.service.OaActEventPublisherService;
import com.vito.oa.act.extend.service.OaActExMsgService;
import com.vito.oa.act.extend.service.OaActProgressTableService;
/***
 * 整个流程结束监听
 */
public class ActivitiExecutionEndListener implements Serializable, ExecutionListener {
	private static final long serialVersionUID = 3157803973501745204L;
	private static Logger log = LoggerFactory.getLogger(ActivitiExecutionEndListener.class);

	@SuppressWarnings("unchecked")
	@Override
	public void notify(DelegateExecution execution) throws Exception {
		ExecutionEntity entity = (ExecutionEntity) execution;

//		Map<String, Object> originInfoMap = (Map<String, Object>) entity.getVariable(OaActContants.ORIGIN_TRANSITION);
//		if (null != originInfoMap) {
//			OaActivitiTool.restoreTransition((ActivityImpl) originInfoMap.get(OaActContants.ORIGIN_TRANSITION_ACT),
//					(List<PvmTransition>) originInfoMap.get(OaActContants.ORIGIN_TRANSITION_TRANS));
//		}
//		entity.setVariable(OaActContants.ORIGIN_TRANSITION, null);

		String processkey = entity.getProcessDefinition().getKey();
		String businessId = execution.getProcessBusinessKey();
		log.debug("流程{}启结束,业务id[{}]", processkey, execution.getProcessBusinessKey());
		OaActProgressTableService actProgressTableService = SpringApplicationContext
				.getApplicationContextBean(OaActProgressTableService.class);
		String endSign = (String) entity.getVariable(Contants.ENDPROCESS_KEY);
		actProgressTableService.updateProgressToEnd(processkey, execution.getProcessBusinessKey(), endSign);

		// 根据流程key检查是否需要发消息
		OaActProcessMapper processMapper = SpringApplicationContext.getApplicationContextBean(OaActProcessMapper.class);
		String syskey = processMapper.queryOriginalProcessKey(processkey);
		if (Validata.isNullOrEmpty(syskey)) {
			syskey = processkey;
		}
		if (ActivitiEvent.END_EVENT_KEY.contains(syskey)) {
			this.sendMessage(businessId, syskey, processkey,
					(endSign == null || "".equals(endSign)) ? Contants.PROCESS_STATUS_END : endSign);
		}

		// 流程结束消息通知
		OaActExMsgService actExMsgService = SpringApplicationContext.getApplicationContext()
				.getBean(OaActExMsgService.class);
		actExMsgService.sendActExMsg(null, null, processkey, execution.getProcessBusinessKey());
	}

	private void sendMessage(String businessId, String sysKey, String processkey, String status) {
		if (StringUtil.isEmpty(businessId) || StringUtil.isEmpty(processkey)) {
			return;
		}
		OaActEventPublisherService publishService = SpringApplicationContext
				.getApplicationContextBean(OaActEventPublisherService.class);
		publishService.sendActEvent(businessId, sysKey, processkey, status);
	}
}
